#include <iostream>
#include <string>
#include <windows.h>
#include <tlhelp32.h>

DWORD FindProcessId(char *processName);

int main()
{
#ifdef FILEIO
  freopen("in.txt","r",stdin); 
  freopen("out.txt","w",stdout); 
#endif
  
  char *processName = (char *)malloc(260);
  memset(processName,0,260);

  printf("Enter the process name: ");
  scanf("%s",processName);
  printf("%s\n",processName);
	
  DWORD processID = FindProcessId(processName);

#ifdef FILEIO
  fclose(stdin);
  fclose(stdout);
#endif
  return 0;
}

DWORD FindProcessId(char *processName)
{
  PROCESSENTRY32 processInfo;
  processInfo.dwSize = sizeof(processInfo);

  HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  if ( processesSnapshot == INVALID_HANDLE_VALUE )
    return 0;

  Process32First(processesSnapshot, &processInfo);
  if ( strcmp(processName,processInfo.szExeFile) == 0)
    {
      //      CloseHandle(processesSnapshot);
      //      return processInfo.th32ProcessID;
      printf("%s has pid %d\n",processName,processInfo.th32ProcessID);      
    }

  while ( Process32Next(processesSnapshot, &processInfo) )
    {
      if (strcmp(processName,processInfo.szExeFile) == 0)
        {
          //          CloseHandle(processesSnapshot);
          printf("%s has pid %d\n",processName,processInfo.th32ProcessID);
        }
    }
	
  CloseHandle(processesSnapshot);
  return 0;
}









